package managedbean.SCM.Batch;

import entity.RawMaterial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SCM.BatchBeanLocal;

@ManagedBean(name = "addMaterialBatchManagerBean")
@ViewScoped
public class AddMaterialBatchManagerBean implements Serializable{

    public AddMaterialBatchManagerBean() {
    }
    
    @EJB
    private BatchBeanLocal batchBeanLocal;
    private String materialType;
    private Date expiryDate;
    private Long quantity;
    private String location;
    private String statusMessage;

    public Date getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(Date expiryDate) {
        this.expiryDate = expiryDate;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getMaterialType() {
        return materialType;
    }

    public void setMaterialType(String materialType) {
        this.materialType = materialType;
    }

    public Long getQuantity() {
        return quantity;
    }

    public void setQuantity(Long quantity) {
        this.quantity = quantity;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public List<String> getDisplayMaterialType(){
        List<RawMaterial> rawMaterialList = batchBeanLocal.getMaterial();
        List<String> materialTypeList = new ArrayList<String>();
        for(int i=0; i<rawMaterialList.size(); i++){
            materialTypeList.add(rawMaterialList.get(i).getMaterialType());
        }
        return materialTypeList;
    }
    
    public void saveNewMaterialBatch(ActionEvent event) {
        
        try 
        {
            System.out.println(location);
            System.out.println(materialType);
            Long id = batchBeanLocal.createMaterialBatch(materialType, expiryDate, quantity, location);
            
            if(id == null){
                statusMessage = "Please Try Again.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Unsuccessful:" + statusMessage + "", ""));
            }else{
                statusMessage = "New Material Batch " + id + " Saved Successfully.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
            }
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
    }
    
}
